﻿Public Class ExamMgm
    Private examName As String = "%%"
    Private startTime As DateTime
    Private endTime As DateTime

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        examName = "%" + txtExamName.Text + "%"
        startTime = dtpFromTime.Value
        endTime = dtpToTime.Value
        Try
            Me.ExamTableAdapter1.FillByNameAndTime(Me.ExamDataSet1.exam, examName, startTime, endTime)
            If Me.ExamDataSet1.exam.Count < 1 Then
                System.Windows.Forms.MessageBox.Show("没有查到符合要求的试卷。")
            End If
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub dgvExam_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles dgvExam.DataBindingComplete
        Dim r As DataGridViewRow
        If dgvExam.RowCount >= 1 Then
            For Each r In dgvExam.Rows
                r.Cells(8).Value = "编辑"
                r.Cells(8).Tag = r.Cells(0).Value
                r.Cells(9).Value = "删除"
                r.Cells(9).Tag = r.Cells(0).Value
                r.Cells(10).Value = "查看成绩"
                r.Cells(10).Tag = r.Cells(0).Value
                If r.Cells(3).Value = 0 Then
                    r.Cells(4).Value = "否"
                Else
                    r.Cells(4).Value = "是"
                End If
            Next r
        End If
    End Sub

    Private Sub btnCreateNew_Click(sender As Object, e As EventArgs) Handles btnCreateNew.Click
        Dim create As CreateExam = New CreateExam()
        create.Owner = Me
        create.ShowDialog()
        Me.ExamTableAdapter1.Fill(Me.ExamDataSet1.exam)
    End Sub


    Private Sub ExamMgm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.ExamTableAdapter1.Fill(Me.ExamDataSet1.exam)
        Dim dt As DateTime = DateTime.Now
        Dim hour As Integer = dt.Hour
        Dim min As Integer = dt.Minute
        Dim seconds As Integer = dt.Second + 1
        dt = dt.AddHours(-hour)
        dt = dt.AddMinutes(-min)
        dt = dt.AddSeconds(-seconds)
        dtpToTime.Value = dt.AddDays(1)
        dtpFromTime.Value = dt.AddDays(-7)
        startTime = dtpFromTime.Value
        endTime = dtpToTime.Value
    End Sub

    Private Sub ExamMgm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        End
    End Sub


    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs)
        End
    End Sub

    Private Sub QuestionToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Me.Hide()
        SelectQuestions.Show()
    End Sub

    Private Sub StudentMgmToolStripMenuItem1_Click(sender As Object, e As EventArgs)
        Me.Hide()
        StudentMgm.Show()
    End Sub

    Private Sub dgvExam_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvExam.CellClick
        If e.RowIndex >= 0 Then
            If e.ColumnIndex = 8 Then
                Dim examId As Integer = Integer.Parse(dgvExam.SelectedCells(0).Tag.ToString())
                Dim editExam As CreateExam = New CreateExam()
                editExam.ExamIdToUpdate = examId
                editExam.ShowDialog()
                Me.ExamTableAdapter1.Fill(Me.ExamDataSet1.exam)

            ElseIf e.ColumnIndex = 9 Then
                Dim examId As Integer = Integer.Parse(dgvExam.SelectedCells(0).Tag.ToString())
                If MsgBox("是否删除该试卷？", MsgBoxStyle.YesNo, "警告") = MsgBoxResult.Yes Then
                    Try
                        Me.ExamTableAdapter1.DeleteByExamId(examId)
                        Me.Stu_examTableAdapter1.DeleteByExamId(examId)
                        Me.Exam_questionsTableAdapter1.DeleteByExamId(examId)
                        MessageBox.Show("删除试卷成功！")
                        Me.ExamTableAdapter1.Fill(Me.ExamDataSet1.exam)
                    Catch ex As Exception
                        MessageBox.Show("删除试卷失败！" + ex.Message)
                    End Try
                End If
            ElseIf e.ColumnIndex = 10 Then
                Dim examId As Integer = Integer.Parse(dgvExam.SelectedCells(0).Tag.ToString())
                Dim showScore As StuScore = New StuScore()
                showScore.CheckExamId = examId
                showScore.ShowDialog()
            End If
        End If

    End Sub
End Class